import {GeneratedType, Registry} from "@cosmjs/proto-signing";

require("dotenv").config();
const { DirectSecp256k1HdWallet } = require("@cosmjs/proto-signing")
const { SigningStargateClient, StargateClient, defaultRegistryTypes } = require("@cosmjs/stargate")
const { stringToPath } = require("@cosmjs/crypto")
const { MsgDepositForBurnWithCaller,MsgDepositForBurn } = require("../generated/circle/cctp/v1/tx")

const config = require('../config');
const {    eth_caller_wallet,
    fn_noble_caller,
    fn_noble_receipter} = require("../wallet");

export const cctpTypes: ReadonlyArray<[string, GeneratedType]> = defaultRegistryTypes.concat(
  [  ["/circle.cctp.v1.MsgDepositForBurnWithCaller", MsgDepositForBurnWithCaller],
    ["/circle.cctp.v1.MsgDepositForBurn", MsgDepositForBurn]
  ]
)

function createDefaultRegistry(): Registry {
    return new Registry(cctpTypes)
}

function  address_eth_to_noble(address_eth){

    const cleanedMintRecipient = address_eth.replace(/^0x/, '');
    const zeroesNeeded = 64 - cleanedMintRecipient.length;
    const mintRecipient = '0'.repeat(zeroesNeeded) + cleanedMintRecipient;
    console.log("mintRecipient: ", mintRecipient)
    const buffer = Buffer.from(mintRecipient, "hex")
    const mintRecipientBytes = new Uint8Array(buffer);
    return mintRecipientBytes;
}

const main = async ()=>{
    let caller_wallet = await fn_noble_caller();
    const [caller_account] = await caller_wallet.getAccounts();
    console.log('caller_account: ', caller_account.address);

    const client_caller = await SigningStargateClient.connectWithSigner(
        config.noble.url,
        caller_wallet,
        {
            registry: createDefaultRegistry()
        }
    );

    const eth_acceipter = config.goerli.accounts.receipter;


    const mintRecipientBytes_acceipter = address_eth_to_noble(eth_acceipter);

    const eth_caller = config.goerli.accounts.caller;

    const caller_bytes = address_eth_to_noble(eth_caller);



    const msg = {
        typeUrl: "/circle.cctp.v1.MsgDepositForBurn",
        value: {
            from: caller_account.address,
            amount: "12345",
            destinationDomain: 0,
            mintRecipient: mintRecipientBytes_acceipter,
            burnToken: "uusdc",


        }
    }

    const sendMsg = {
        typeUrl: "/cosmos.bank.v1beta1.MsgSend",
        value:{
            fromAddress: caller_account.address,
            toAddress:'noble102rkzjw0p7a6t27ywsgggj2mjhzrarqlu66ys2',
            amount: [
                {
                    denom: "uusdc",
                    amount: "100",
                },
            ],

        }
    }
    const fee = {
        amount: [
            {
                denom: "uusdc",
                amount: "100",
            },
        ],
        gas: "200000",
    };
    const memo = "haolf";



    const result = await client_caller.signAndBroadcast(
        caller_account.address,
        [sendMsg,msg],
        fee,
        memo
    );
    console.log("Burned on Noble to to https://testnet.mintscan.io/noble-testnet/tx/" + result.transactionHash)

}
main();

/*
* https://testnet.mintscan.io/noble-testnet/txs/145D85359293F659DFC8341FE839B8B818EBD55F1780E976BED416F78F5473F6
* */